$ScriptDir = (Get-Item -Path $MyInvocation.MyCommand.Path | Select-Object -ExpandProperty DirectoryName)
. "$ScriptDir\includes\test_utils.ps1"
. "$ScriptDir\includes\log_utils.ps1"
# 导出密码策略规则
$PolicyFileName = "security_policy.txt"
$PolicyFilePath = Join-Path -Path $PSScriptRoot -ChildPath $PolicyFileName

secedit /export /cfg $PolicyFilePath 2>&1 > $null



# set-PSDebug -Trace 2

function FuncWrapper {
    param(
        [string]$func,
        [string[]]$args
    )

    & $func $args
    $result = $global:result

    if ($result -eq 127) {
        info "$func not implemented"
    } elseif ($result -eq 0) {
        access "$func"
    } elseif ($result -eq 1) {
        error "$func"
    } elseif ($result -eq 2) {
        warn "$func"
    }
}

function Main {
    info "0 indicates compliance, 1 indicates non-compliance, 2 indicates that the rule does not exist"
    $rulesScriptPath = Join-Path -Path $ScriptDir -ChildPath "rules.ps1"
    # 导入规则脚本
    . $rulesScriptPath
    $ruleFunctions = Get-Content "$ScriptDir\rules.ps1" | Select-String "function [A-Za-z0-9_]*" | ForEach-Object { $_.Matches[0].Value.Trim() -replace "function\s", "" }                 
    foreach ($func in $ruleFunctions) {
        $functionDefinition = (Get-Content "$ScriptDir\rules.ps1" | Out-String) -split "`n" | Where-Object { $_ -match "function $func" }
        
        if ($functionDefinition -notmatch "^\s*#") {
            FuncWrapper $func
        } 
    }
}

Main
